package com.wsq.aidlclient;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.wsq.aidlservice.aidl.IServiceAidlInterface;
//import com.xzl.tensor.aidl.ICommunicationInterface;

public class MainActivity extends AppCompatActivity {
    private IServiceAidlInterface iServiceAidlInterface;

    private TextView textView;
    int i = 0;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Application application;

        textView = findViewById(R.id.text);

        Log.e("client", "onCreate");


        Handler handler = new Handler(getMainLooper());

        Log.e("client", "启动了");

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("点击了：" + i++);
            }
        });

        findViewById(R.id.aidlText).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("等待服务端返回数据");
                int data = 0;
                Log.e("client", "java 线程id：" +android.os.Process.myTid());
                try {
                    data = iServiceAidlInterface.getData(1);
                } catch (RemoteException e) {
                    throw new RuntimeException(e);
                }
                Log.e("client", "服务端返回数据为：" + data);
                textView.setText("服务端返回数据为：" + data);
            }
        });

//        Intent intent1 = new Intent();
//        intent1.setAction("android.intent.action.RECIEVE_ADID");
//        intent1.setPackage("com.xzl.tensor");
//        bindService(intent1, serviceConnection1, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = new Intent();
        intent.setAction("wsq.aidl.service");
        intent.setPackage("com.wsq.aidlservice");
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);



    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e("client", "已经绑定");
            Log.e("client", "java 线程id：" +android.os.Process.myTid());
            iServiceAidlInterface = IServiceAidlInterface.Stub.asInterface(service);
            try {
                iServiceAidlInterface.basicTypes(1,12,true,1.11F, 1.12, "chiji");

            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e("client", "链接断开");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
//        unbindService(serviceConnection1);
    }
}
